package com.ATMgui;

import com.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SaveMoney extends JPanel implements ActionListener {
    private Icon sureBtn = Util.getIcon("/image/buttonSure.jpg");
    private JButton sure;
    private ATM frame;
    private JTextField textField1;

    public JTextField getTextField1() {
        return textField1;
    }

    public void setTextField1(JTextField textField1) {
        this.textField1 = textField1;
    }

    public SaveMoney(ATM frame) {
        this.frame = frame;

        this.setLayout(null);
        textField1 = new JTextField();
        textField1.setBounds(250, 150, 250, 30);
        this.add(textField1);

        this.setOpaque(false);
        sure = new JButton(sureBtn);
        sure.setBounds(200, 320, 100, 20);
        this.add(sure);
        sure.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sure) {
            DBUtil db = new DBUtil();
            Users users = db.getById(frame.getLoginUser().getId());
            users.setBlance(Double.valueOf(textField1.getText())+users.getBlance());
            db.update(users);
//            Double money = Double.valueOf(textField1.getText()) + frame.getUsers().get(frame.getNum()).getBlance();
//            frame.getUsers().get(frame.getNum()).setBlance(money);
//            String sql = "UPDATE t_users SET u_blance="+frame.getUsers().get(frame.getNum()).getBlance()+" where u_name='"+frame.getUsers().get(frame.getNum()).getUserName()+"'";
            JOptionPane.showMessageDialog(this, "存款成功");
            frame.switchPanel("菜单");
        }
    }
}
